package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyArea;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyAreaMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyAreaParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyAreaResult;
import  cn.stylefeng.guns.sys.modular.system.service.BtyAreaService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxd
 * @since 2022-07-20
 */
@Service
public class BtyAreaServiceImpl extends ServiceImpl<BtyAreaMapper, BtyArea> implements BtyAreaService {

    @Override
    public void add(BtyAreaParam param){
        BtyArea entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BtyAreaParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyAreaParam param){
        BtyArea oldEntity = getOldEntity(param);
        BtyArea newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BtyAreaResult findBySpec(BtyAreaParam param){
        return null;
    }

    @Override
    public List<BtyAreaResult> findListBySpec(BtyAreaParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyAreaParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BtyAreaParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyArea getOldEntity(BtyAreaParam param) {
        return this.getById(getKey(param));
    }

    private BtyArea getEntity(BtyAreaParam param) {
        BtyArea entity = new BtyArea();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public void batchSwitchStatus(BtyAreaParam param) {
        BtyArea btyArea = new BtyArea();
        btyArea.setIsCompliance(param.getIsCompliance());

        String[] idsArr = param.getIds().split(",");
        List<String> idsList = Arrays.asList(idsArr);
        QueryWrapper qw = new QueryWrapper();
        qw.in("id", idsList);
        this.update(btyArea, qw);
    }

    @Override
    public List<BtyArea> ddList(BtyAreaParam param) {
        QueryWrapper qw = new QueryWrapper();
        qw.select("distinct dd").eq("yyjl", param.getYyjl());
        return this.baseMapper.selectList(qw);
    }

    @Override
    public List<BtyArea> getShopByYyjl(BtyAreaParam param) {
        QueryWrapper qw = new QueryWrapper();
        qw.select("distinct dm");
        qw.eq("yyjl", param.getYyjl());
        if (ObjectUtil.isNotEmpty(param.getDd())) {
            qw.eq("dd", param.getDd());
        }
        return this.baseMapper.selectList(qw);
    }

}
